// (c) 2015 Teresa Holfeld
package com.holfeld.teresa.vocabtrainer;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.CardView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.holfeld.teresa.vocabtrainer.card.AddVocableCard;
import com.holfeld.teresa.vocabtrainer.card.AddVocabularyCard;
import com.holfeld.teresa.vocabtrainer.card.CardTextWatcher;
import com.holfeld.teresa.vocabtrainer.provider.VocabularyPool;

/**
 * Activity for adding a new Vocabulary. Presents inputs for specifying the flash card stack's name
 * and its locales. By clicking save, inputs for a flash card are shown, where the user can specify
 * the word in both locales. Clicking save always shows the next flash card. Clicking cancel cancels
 * this task, and the user can save the whole stack.
 *
 * @author Teresa Holfeld, teresa.holfeld@gmail.com
 */
public class AddVocabularyActivity extends ActionBarActivity
        implements AddVocableCard.ButtonCallback, AddVocabularyCard.ButtonCallback {

    private String mVocabularyName;

    /**
     * The layout that holds the input cards.
     */
    private LinearLayout mActivityLayout;

    /**
     * The button bar with the "Done" and "Add" button.
     */
    private LinearLayout mActionButtonBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_vocabulary);

        mActivityLayout = (LinearLayout) findViewById(R.id.add_vocabulary_layout);
        mActionButtonBar = (LinearLayout) findViewById(R.id.actionButtonBar);
        mActionButtonBar.setVisibility(View.GONE);
        initActionButtons();
        initVocabularyInfoCard();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_add_vocabulary, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void initActionButtons() {
        Button doneButton = (Button) mActionButtonBar.findViewById(R.id.done);
        Button addButton = (Button) mActionButtonBar.findViewById(R.id.add);
        doneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addVocableCard();
                mActionButtonBar.setVisibility(View.GONE);
            }
        });
    }

    /**
     * Initializes the functionality of the CardView holding the inputs for title and locales.
     * Creates a CardTextWatcher for the three inputs, and enables the save button only if the
     * values are changed. Sets the OnClickListeners for the save and cancel button.
     */
    private void initVocabularyInfoCard() {
        AddVocabularyCard addVocabularyCard = new AddVocabularyCard(this);
        mActivityLayout.addView(addVocabularyCard);
    }

    /**
     * Adds a CardView for a new vocable, containing two TextViews for the word in both locales.
     * Creates a CardTextWatcher for the three inputs, and enables the save button only if the
     * values are changed. Sets the OnClickListeners for the save and cancel button.
     */
    private void addVocableCard() {
        AddVocableCard addVocableCard = new AddVocableCard(this, mVocabularyName);
        mActivityLayout.addView(addVocableCard);
        addVocableCard.focus();
    }

    /**
     * Checks whether the given CardView is the last one in the parent layout.
     *
     * @param cardView the CardView to check
     * @return true if the given CardView is the last one in the parent layout
     */
    private boolean isLastCard(CardView cardView) {
        int numberCards = mActivityLayout.getChildCount();
        int cardIndex = mActivityLayout.indexOfChild(cardView);
        return cardIndex == numberCards - 1;
    }

    /**
     * Checks whether the last CardView in the layout is not an empty AddVocableCard.
     *
     * @return true if the last card is not empty
     */
    private boolean lastCardIsNotEmpty() {
        int numberCards = mActivityLayout.getChildCount();
        CardView lastCardView = (CardView) mActivityLayout.getChildAt(numberCards - 1);
        if (lastCardView instanceof AddVocabularyCard) {
            return true;
        }
        return !((AddVocableCard) lastCardView).isEmpty();
    }

    private void changeVocabularyName(String newName) {
        mVocabularyName = newName;
        if (mActivityLayout.getChildCount() > 1) {
            for (int i = 1; i < mActivityLayout.getChildCount(); i++) {
                AddVocableCard addVocableCard = (AddVocableCard) mActivityLayout.getChildAt(i);
                addVocableCard.changeVocabularyName(newName);
            }
        }
    }

    @Override
    public void onSaveVocabulary(AddVocabularyCard addVocabularyCard) {
        String vocabularyName = addVocabularyCard.getVocabularyName();
        if (mVocabularyName == null || !mVocabularyName.equals(vocabularyName)) {
            changeVocabularyName(vocabularyName);
        }
        if (isLastCard(addVocabularyCard)) {
            addVocableCard();
        }
    }

    @Override
    public void onCancelVocabulary(AddVocabularyCard addVocabularyCard) {
        finish();
    }

    @Override
    public void onSaveVocable(AddVocableCard addVocableCard) {
        if (isLastCard(addVocableCard)) {
            addVocableCard();
        }
    }

    @Override
    public void onCancelVocable(AddVocableCard addVocableCard) {
        // TODO animation
        mActivityLayout.removeView(addVocableCard);
        // if the last item is not empty, show the action button bar
        if (lastCardIsNotEmpty()) {
            mActionButtonBar.setVisibility(View.VISIBLE);
        }
    }
}
